<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\AskRequest;

class AskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //申请救助列表
    public function getIndex(Request $request)
    {
         //每页显示
        $num = $request->input('num',8);
        //判断是否有关键词
        if($request->input('keywords')){

            $cates = DB::table('ask')->where('tname','like','%'.$request->input('keywords').'%')->paginate($num);

        }else{

            //查询数据
            $cates = DB::table('ask')->paginate($num);
            
        }

        $data = $request->except('page');
        // dd($data);
        return view('Admin.ask.index',['cates'=>$cates,'data'=>$data]);
    }

    //救助类别添加
    public function getAdd()
    {
        // echo 33;
        return view('Admin.ask.add');
    }

    //类别添加操作

    public function postInsert(Request $request)
    {
        // dd($request->all());
        $data = $request->except('_token');

    public function postInsert(AskRequest $request)
    {
        // dd($request->all());
        $data = $request->except('_token');
        $data['pic']=$this->upload($request);


        $res = DB::table('ask')->insert($data);

        if($res){
            return redirect('admin/ask/index')->with('success','添加成功');
        }else{

            return back()->with('error','添加失败');
        }
    }



      //文件上传
    public function upload($request)
    {
            // $type = $request ->file('pic')->mimeType;
            // dd($request);
        // var_dump($request);
        $arr = ['jpg','png','jpeg','gif'];

        if($request->file('pic'))
        {

            // var_dump($this->$mimeType);
            $t = $request->file('pic')->guessExtension();

            if(!in_array($t,$arr)){
                echo '文件类型不符合';
            }

        //随机文件名
        $name = md5(time()+rand(1,99999));
        //后缀名
        $su = $request->file('pic')->getClientOriginalExtension();
        $request->file('pic')->move('./public/articleuploads', $name.'.'.$su);

        return '/public/articleuploads/'.$name.'.'.$su;


        }
    }


    //显示状态修改
    public function getChange($id)
    {
        
        //查询当前状态
        $res = DB::table('ask')->where('id',$id)->first();

       //判断
        if($res->status == 1)
        {
            $data['id'] =$id;
            $data['status'] = 0;

           $num = DB::table('ask')->where('id',$id)->update($data);

        }else{

            $data['id'] =$id;
            $data['status'] = 1;

           $num = DB::table('ask')->where('id',$id)->update($data);

        }

        if($num){
            return redirect('/admin/ask/index')->with('success','显示状态修改成功');
        }else{
             return back()->with('error','显示状态修改失败');
        }
    }


   //类别删除操作
    public function postDel(Request $request)
    {
         $res = DB::table('ask')->where('id','=',$request->id)->delete();
         echo $res;
    }

    //修改操作
    public function getEdit($id)
    {
        //查询数据
        $res = DB::table('ask')->where('id',$id)->first();

        return view('Admin.ask.edit',['res'=>$res]);
    }

    //修改操作

    public function postUpdate(Request $request)
    {
        $data = $request->except('_token');

    public function postUpdate(AskRequest $request)
    {
        $data = $request->except('_token');
        $data['pic'] = $this->upload($request);

        $res = DB::table('ask')->where('id',$request->id)->update($data);

        if($res){
            return redirect('/admin/ask/index')->with('success','修改成功');
        }else{

            return back()->with('error','修改失败');
        }
    }
}
